package use_case.processRental;

import java.io.IOException;
import java.util.ArrayList;

import services.interfaces.IBillingServiceAdapter;
import services.interfaces.ServicesFactory;
import use_case.handlereturns.*;
public class Kiosk {
	
	//Instance of a Class
	private Sale newSale;
	private Ledger kioskLedger;
	private Storage storage;
	private Slot slot;
	private cardReader credit;
	private IBillingServiceAdapter billingServiceInterface;
	
	//Constructor
	public Kiosk(Storage storage, Ledger kioskLedger)
	{
		//Assign Reference
		this.kioskLedger = kioskLedger;
		this.storage = storage;
		
		//Create Object of Class
		slot = new Slot(storage);
		credit =  new cardReader();
		
	}
	
	//***********************************************
	//Use Case Handle Returns
	public Boolean returnMode(itemDescription item)
	{
		// If item is inserted into storage
		// Then return true
		if(slot.inputItem(item)){
			System.out.println("ITEM RETURNED SUCCESSFULLY!");
			return true;
		}
		else{
			System.out.println("INVALID RETURN!!");
			return false;
		}
	}
	//***********************************************
	
	
	//***********************************************
	//Use Case Process Rental
	public void rentMode()
	{
		//Create Sale Object and
		Sale newSale = new Sale(storage);
		this.newSale = newSale;

	}
	
	public void addItemToCart(int itemNumber){	
		//Add Item From Inventory to Shopping Cart
		newSale.addItem(itemNumber);
	}
	
	public boolean makePayment(Customer customer)
	{
		//Verify the CardNumber is valid
		if(credit.receiveCardNumber(customer.getCardNumber()))
		{	
			recordSale(customer);
			return true;
		}
		else{
			return false;
		}
	}
	
	public void removeItemFromCart(int itemToRemove) {
		// Remove Item From Shopping Cart
		newSale.removeItemFromCart(itemToRemove);
		
	}
	
	//***********************************************
	
	//***********************************************
	//Utility Functions
	public Sale getSale(){
		//Return Current Sale Object
		return newSale;
	}
	

	public String[] getSelectedList(){
		//Return List of items from Shopping Cart
		return newSale.getSelectedList();
	}
	//***********************************************
	
	
	//Record Current Sale in Kiosk Ledger
	//Record current Sale and Customer in CatalogService
	public void recordSale(Customer customer){
		
		kioskLedger.addSale(newSale);
		kioskLedger.addCustomer(customer);
		
		billingServiceInterface = ServicesFactory.getInstance().getBillingServiceAdapter();
		billingServiceInterface.addRentalRecord(customer, newSale);
		
		System.out.println("\nSale Recorded");	
	}

}
